package com.cssnj.portal.web.admin.editfunction;

import com.cssnj.portal.service.admin.editfunction.EditfunctionService;
import com.cssnj.portal.service.common.TreeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.domain.exception.BaseSysException;
import io.predictech.framework.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

@Controller
@RequestMapping({"/admin/editfunction"})
public class EditfunctionController {

    @Autowired
    private EditfunctionService editfunctionService;

    @Autowired
    private TreeService treeService;

    @RequestMapping({"/init.do"})
    public ModelAndView init(HttpServletRequest request) throws BaseException, SQLException {
        //页面初始化
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("treeService");
        jsonRequest.setHandleCode("allTree");
        JsonResponse jsonResponse = treeService.execute(jsonRequest);
        ModelAndView modelAndView = new ModelAndView("admin/editfunction/editfunction");
        if (null != jsonResponse.getRepData().get("treeList")) {
            try {
                ObjectMapper objectMapper = StringUtil.getBaseObjectMapper();
                modelAndView.getModelMap().put("str_gnsList", objectMapper.writeValueAsString(jsonResponse.getRepData().get("treeList")));
            } catch (JsonProcessingException e) {
                throw new BaseSysException(e.getMessage());
            }
        }
        return modelAndView;
    }

    @RequestMapping({"/updateWjj.do"})
    @ResponseBody
    public JsonResponse updateWjj(HttpServletRequest request) throws BaseException, SQLException {
        //修改文件夹
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("editfunctionService");
        jsonRequest.setHandleCode("updateWjj");
        jsonRequest.getReqData().put("id", request.getParameter("id"));
        jsonRequest.getReqData().put("mc", request.getParameter("mc"));
        jsonRequest.getReqData().put("zt", request.getParameter("zt"));
        jsonRequest.getReqData().put("wjjwz", request.getParameter("wjjwz"));
        JsonResponse jsonResponse = editfunctionService.execute(jsonRequest);
        return jsonResponse;
    }

    @RequestMapping({"/addWjj.do"})
    @ResponseBody
    public JsonResponse addWjj(HttpServletRequest request) throws BaseException, SQLException {
        //根据父节点获取新文件夹的排序号
        JsonRequest jsonRequest = new JsonRequest();
        String pid = request.getParameter("pid");
        String mc = request.getParameter("mc");
        String zt = request.getParameter("zt");
        jsonRequest.setServiceName("treeService");
        jsonRequest.setHandleCode("getGnChildPx");
        jsonRequest.getReqData().put("sjgn_id", request.getParameter("pid"));
        JsonResponse jsonResponse = treeService.execute(jsonRequest);
        String xh = (String) jsonResponse.getRepData().get("nextpx");
        //增加文件夹
        jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("editfunctionService");
        jsonRequest.setHandleCode("addWjj");
        jsonRequest.getReqData().put("pid", pid);
        jsonRequest.getReqData().put("mc", mc);
        jsonRequest.getReqData().put("zt", zt);
        jsonRequest.getReqData().put("xh", xh);
        jsonResponse = editfunctionService.execute(jsonRequest);
        return jsonResponse;
    }

    @RequestMapping({"/updateCdxx.do"})
    @ResponseBody
    public JsonResponse updateCdxx(HttpServletRequest request) throws BaseException, SQLException {
        //修改菜单信息
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("editfunctionService");
        jsonRequest.setHandleCode("updateCdxx");
        jsonRequest.getReqData().put("id", request.getParameter("id"));
        jsonRequest.getReqData().put("mc", request.getParameter("mc"));
        jsonRequest.getReqData().put("zt", request.getParameter("zt"));
        jsonRequest.getReqData().put("dkfs", request.getParameter("dkfs"));
        jsonRequest.getReqData().put("cdwz", request.getParameter("cdwz"));
        JsonResponse jsonResponse = editfunctionService.execute(jsonRequest);
        return jsonResponse;
    }


    @RequestMapping({"/updateGnLj.do"})
    @ResponseBody
    public JsonResponse updateGnLj(HttpServletRequest request) throws BaseException, SQLException {
        //修改菜单位置
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("editfunctionService");
        jsonRequest.setHandleCode("updateGnLj");
        jsonRequest.getReqData().put("id", request.getParameter("id"));
        jsonRequest.getReqData().put("pid", request.getParameter("pid"));
        JsonResponse jsonResponse = editfunctionService.execute(jsonRequest);
        return jsonResponse;
    }

    @RequestMapping({"upNodeWz.do"})
    @ResponseBody
    public JsonResponse upNode(HttpServletRequest request) throws BaseException, SQLException {
        //两个菜单更改上下位置
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.setServiceName("treeService");
        jsonRequest.setHandleCode("jiaoHuan");
        jsonRequest.getReqData().put("gn_id_1", request.getParameter("node1"));
        jsonRequest.getReqData().put("gn_id_2", request.getParameter("node2"));
        JsonResponse jsonResponse = treeService.execute(jsonRequest);
        return jsonResponse;
    }
}
